package com.fred.colorpicker.indicator

import android.content.Context
import android.content.SharedPreferences
import android.graphics.Canvas
import android.graphics.PointF
import android.os.Build
import android.util.AttributeSet
import android.util.Log
import android.view.MotionEvent
import android.view.View
import androidx.annotation.ColorInt
import androidx.annotation.RequiresApi
import androidx.annotation.Size
import com.fred.colorpicker.GraySeekBarBean
import com.fred.colorpicker.dip

abstract class ColorWheelIndicator(context: Context) : View(context) {

    protected val paddingSize by lazy {
        context.dip(4F)
    }

    protected val currentPoint by lazy { PointF() }
    protected var defaultHRatio = 0.55F
    protected var defaultVRatio = 0.85F
    protected var defaultGrayRatio = 0.85F
    protected var defaultColor = -2687093
    protected var defaultSaturation = 0.23F
    protected var defaultBrightness = 0.77F

    /**
     * 标记是否绘制中心
     */
    var isDrawCentre = true


    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        canvas?.apply {
            drawSelector(this, currentPoint)
        }
    }

    override fun setOnClickListener(l: OnClickListener?) {
        super.setOnClickListener(l)
        isSelected = true
    }

    override fun onTouchEvent(event: MotionEvent?): Boolean {
//        return super.onTouchEvent(event)
        return false
    }

    fun setCurrentPoint(point: PointF) {
        currentPoint.set(point)
        invalidate()
    }

    fun initColorWheelPosition(w:Int, h:Int){
        val settings: SharedPreferences = context.getSharedPreferences(
                "colorPicker",
                Context.MODE_PRIVATE
        )
        val name = javaClass.name
        val hRatio = settings.getFloat(name+"hRatio",defaultHRatio)
        val vRatio = settings.getFloat(name+"vRation",defaultVRatio)
        currentPoint.set(hRatio*w,vRatio*h)
        invalidate()
    }

    fun setColorWheelConfig(hRatio: Float, vRatio: Float){
        val settings: SharedPreferences = context.getSharedPreferences(
            "colorPicker",
            Context.MODE_PRIVATE
        )
        val edit = settings.edit()
        val name = javaClass.name
        edit.putFloat(name + "hRatio", hRatio)
        edit.putFloat(name + "vRation", vRatio)
        edit.apply()
    }

    fun setGraySeekBarConfig(@ColorInt color:Int,saturation:Float,brightness:Float){
        val settings: SharedPreferences = context.getSharedPreferences(
                "colorPicker",
                Context.MODE_PRIVATE
        )
        val edit = settings.edit()
        val name = javaClass.name
        edit.putInt(name+"color",color)
        edit.putFloat(name+"saturation",saturation)
        edit.putFloat(name+"brightness",brightness)
        edit.apply()
    }

    fun initGraySeekBar(w: Int,y:Float): GraySeekBarBean {
        val settings: SharedPreferences = context.getSharedPreferences(
                "colorPicker",
                Context.MODE_PRIVATE
        )
        val name = javaClass.name
        val ratio = settings.getFloat(name+"grayRatio",defaultGrayRatio)
        currentPoint.x = ratio*w
        currentPoint.y = y

       val color = settings.getInt(name+"color",defaultColor)
        val saturation = settings.getFloat(name+"saturation",defaultSaturation)
        val brightness = settings.getFloat(name+"brightness",defaultBrightness)
        invalidate()
        return GraySeekBarBean(color, saturation, brightness,PointF(currentPoint.x,currentPoint.y))
    }

    fun setGraySeekBarPositionConfig(ratio: Float){
        val settings: SharedPreferences = context.getSharedPreferences(
                "colorPicker",
                Context.MODE_PRIVATE
        )
        val edit = settings.edit()
        val name = javaClass.name
        edit.putFloat(name + "grayRatio", ratio)
        edit.apply()
    }

    /**
     * 判断 (x,y)是否在其内部
     * @param x Float
     * @param y Float
     * @return Boolean
     */
    abstract fun isInside(x: Float, y: Float):Boolean

    abstract fun drawSelector(canvas: Canvas, point: PointF)

    abstract fun minimumWidth():Int

    abstract fun minimumHeight():Int

    abstract fun copy():ColorWheelIndicator

}